﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ttbit.core;

namespace ttbit.WorkingSpace
{

    class Parser_Text : IWorkResourceParser
    {
        readonly string[] __exts = new string[] { ".txt", ".json" };
        public string[] FileExt => __exts;

        public string TypeName => Resource_Text.TypeStr;
        public override string ToString()
        {
            return TypeName + "=>" + __exts[0];
        }
        public bool CanCreate => true;
        public IWorkResource Load(bool reimport, System.IO.Stream metaStream, string path)
        {
            var name = Path.GetFileNameWithoutExtension(path).ToLower();

            var res = new Resource_Text(name);
            //no need to read from meta.

            if (System.IO.File.Exists(path))
            {
                res.Text = System.IO.File.ReadAllText(path);
                res.ClearDirty();
            }
            return res;
        }
        public bool Save(IWorkResource resource, System.IO.Stream metaStream, string path)
        {
            //no need to write to meta.

            var res = resource as Resource_Text;
            //no need to write to data;
            System.IO.File.Delete(path);
            System.IO.File.WriteAllText(path, res.Text);
            return true;
        }

    }
}
